package socketVersion.client;

import java.util.LinkedList;
import java.util.Vector;

import socketVersion.Reply;
import socketVersion.Request;
import socketVersion.GenericClient;
import socketVersion.Middle.MiddleServerInterface;

public class MiddleServerWrapper extends GenericClient implements MiddleServerInterface{

	public MiddleServerWrapper(String ip, int port){
		super(ip,port);
	}

	@Override
	public boolean addFlight(int id, int flightNum, int flightSeats,int flightPrice) throws Exception {
		LinkedList<Object> args = new LinkedList<Object>();
		args.add(id);
		args.add(flightNum);
		args.add(flightSeats);
		args.add(flightPrice);
		
		Request request = new Request("addFlight",args);
		Reply reply = sendRequest(request);
		if(reply.getStatus()==Reply.STATUS_SUCCESS){
			return ((Boolean)reply.getOutput().get(0)).booleanValue();
		}
		else{
			throw new Exception("Request failed, Status: " + reply.getStatus());
		}
	}

	@Override
	public boolean addCars(int id, String location, int numCars, int price) throws Exception {
		LinkedList<Object> args = new LinkedList<Object>();
		args.add(id);
		args.add(location);
		args.add(numCars);
		args.add(price);
		
		Request request = new Request("addCars",args);
		Reply reply = sendRequest(request);
		if(reply.getStatus()==Reply.STATUS_SUCCESS){
			return ((Boolean)reply.getOutput().get(0)).booleanValue();
		}
		else{
			throw new Exception("Request failed, Status: " + reply.getStatus());
		}
	}

	@Override
	public boolean addRooms(int id, String location, int numRooms, int price) throws Exception {
		LinkedList<Object> args = new LinkedList<Object>();
		args.add(id);
		args.add(location);
		args.add(numRooms);
		args.add(price);
		
		Request request = new Request("addRooms",args);
		Reply reply = sendRequest(request);
		if(reply.getStatus()==Reply.STATUS_SUCCESS){
			return ((Boolean)reply.getOutput().get(0)).booleanValue();
		}
		else{
			throw new Exception("Request failed, Status: " + reply.getStatus());
		}
	}

	@Override
	public int newCustomer(int id) throws Exception{
		LinkedList<Object> args = new LinkedList<Object>();
		args.add(id);
		
		Request request = new Request("newCustomer",args);
		Reply reply = sendRequest(request);
		if(reply.getStatus()==Reply.STATUS_SUCCESS){
			return ((Integer)reply.getOutput().get(0)).intValue();
		}
		else{
			throw new Exception("Connection Error Status: " + reply.getStatus());
		}
	}

	@Override
	public boolean newCustomer(int id, int cid) throws Exception {
		LinkedList<Object> args = new LinkedList<Object>();
		args.add(id);
		args.add(cid);
		
		Request request = new Request("newCustomer",args);
		Reply reply = sendRequest(request);
		if(reply.getStatus()==Reply.STATUS_SUCCESS){
			return ((Boolean)reply.getOutput().get(0)).booleanValue();
		}
		else{
			throw new Exception("Request failed, Status: " + reply.getStatus());
		}
	}

	@Override
	public boolean deleteFlight(int id, int flightNum) throws Exception {
		LinkedList<Object> args = new LinkedList<Object>();
		args.add(id);
		args.add(flightNum);
		
		Request request = new Request("deleteFlight",args);
		Reply reply = sendRequest(request);
		if(reply.getStatus()==Reply.STATUS_SUCCESS){
			return ((Boolean)reply.getOutput().get(0)).booleanValue();
		}
		else{
			throw new Exception("Request failed, Status: " + reply.getStatus());
		}
	}

	@Override
	public boolean deleteCars(int id, String location) throws Exception {
		LinkedList<Object> args = new LinkedList<Object>();
		args.add(id);
		args.add(location);
		
		Request request = new Request("deleteCars",args);
		Reply reply = sendRequest(request);
		if(reply.getStatus()==Reply.STATUS_SUCCESS){
			return ((Boolean)reply.getOutput().get(0)).booleanValue();
		}
		else{
			throw new Exception("Request failed, Status: " + reply.getStatus());
		}
	}

	@Override
	public boolean deleteRooms(int id, String location) throws Exception {
		LinkedList<Object> args = new LinkedList<Object>();
		args.add(id);
		args.add(location);
		
		Request request = new Request("deleteRooms",args);
		Reply reply = sendRequest(request);
		if(reply.getStatus()==Reply.STATUS_SUCCESS){
			return ((Boolean)reply.getOutput().get(0)).booleanValue();
		}
		else{
			throw new Exception("Request failed, Status: " + reply.getStatus());
		}
	}

	@Override
	public boolean deleteCustomer(int id, int customer) throws Exception {
		LinkedList<Object> args = new LinkedList<Object>();
		args.add(id);
		args.add(customer);
		
		Request request = new Request("deleteCustomer",args);
		Reply reply = sendRequest(request);
		if(reply.getStatus()==Reply.STATUS_SUCCESS){
			return ((Boolean)reply.getOutput().get(0)).booleanValue();
		}
		else{
			throw new Exception("Request failed, Status: " + reply.getStatus());
		}
	}

	@Override
	public int queryFlight(int id, int flightNumber) throws Exception {
		LinkedList<Object> args = new LinkedList<Object>();
		args.add(id);
		args.add(flightNumber);
		
		Request request = new Request("queryFlight",args);
		Reply reply = sendRequest(request);
		if(reply.getStatus()==Reply.STATUS_SUCCESS){
			return ((Integer)reply.getOutput().getFirst()).intValue();
		}
		else{
			throw new Exception("Connection Error Status: " + reply.getStatus());
		}
	}

	@Override
	public int queryCars(int id, String location) throws Exception {
		LinkedList<Object> args = new LinkedList<Object>();
		args.add(id);
		args.add(location);
		
		Request request = new Request("queryCars",args);
		Reply reply = sendRequest(request);
		if(reply.getStatus()==Reply.STATUS_SUCCESS){
			return ((Integer)reply.getOutput().getFirst()).intValue();
		}
		else{
			throw new Exception("Connection Error Status: " + reply.getStatus());
		}
	}

	@Override
	public int queryRooms(int id, String location) throws Exception {
		LinkedList<Object> args = new LinkedList<Object>();
		args.add(id);
		args.add(location);
		
		Request request = new Request("queryRooms",args);
		Reply reply = sendRequest(request);
		if(reply.getStatus()==Reply.STATUS_SUCCESS){
			return ((Integer)reply.getOutput().getFirst()).intValue();
		}
		else{
			throw new Exception("Connection Error Status: " + reply.getStatus());
		}
	}

	@Override
	public String queryCustomerInfo(int id, int customer) throws Exception {
		LinkedList<Object> args = new LinkedList<Object>();
		args.add(id);
		args.add(customer);
		
		Request request = new Request("queryCustomerInfo",args);
		Reply reply = sendRequest(request);
		if(reply.getStatus()==Reply.STATUS_SUCCESS){
			return ((String)reply.getOutput().getFirst());
		}
		else{
			throw new Exception("Connection Error Status: " + reply.getStatus());
		}
	}

	@Override
	public int queryFlightPrice(int id, int flightNumber) throws Exception {
		LinkedList<Object> args = new LinkedList<Object>();
		args.add(id);
		args.add(flightNumber);
		
		Request request = new Request("queryFlightPrice",args);
		Reply reply = sendRequest(request);
		if(reply.getStatus()==Reply.STATUS_SUCCESS){
			return ((Integer)reply.getOutput().getFirst()).intValue();
		}
		else{
			throw new Exception("Connection Error Status: " + reply.getStatus());
		}
	}

	@Override
	public int queryCarsPrice(int id, String location) throws Exception {
		LinkedList<Object> args = new LinkedList<Object>();
		args.add(id);
		args.add(location);
		
		Request request = new Request("queryCarsPrice",args);
		Reply reply = sendRequest(request);
		if(reply.getStatus()==Reply.STATUS_SUCCESS){
			return ((Integer)reply.getOutput().getFirst()).intValue();
		}
		else{
			throw new Exception("Connection Error Status: " + reply.getStatus());
		}
	}

	@Override
	public int queryRoomsPrice(int id, String location) throws Exception {
		LinkedList<Object> args = new LinkedList<Object>();
		args.add(id);
		args.add(location);
		
		Request request = new Request("queryRoomsPrice",args);
		Reply reply = sendRequest(request);
		if(reply.getStatus()==Reply.STATUS_SUCCESS){
			return ((Integer)reply.getOutput().getFirst()).intValue();
		}
		else{
			throw new Exception("Connection Error Status: " + reply.getStatus());
		}
	}

	@Override
	public boolean reserveFlight(int id, int customer, int flightNumber) throws Exception {
		LinkedList<Object> args = new LinkedList<Object>();
		args.add(id);
		args.add(customer);
		args.add(flightNumber);
		
		Request request = new Request("reserveFlight",args);
		Reply reply = sendRequest(request);
		if(reply.getStatus()==Reply.STATUS_SUCCESS){
			return ((Boolean)reply.getOutput().get(0)).booleanValue();
		}
		else{
			throw new Exception("Request failed, Status: " + reply.getStatus());
		}
	}

	@Override
	public boolean reserveCar(int id, int customer, String location) throws Exception {
		LinkedList<Object> args = new LinkedList<Object>();
		args.add(id);
		args.add(customer);
		args.add(location);
		
		Request request = new Request("reserveCar",args);
		Reply reply = sendRequest(request);
		if(reply.getStatus()==Reply.STATUS_SUCCESS){
			return ((Boolean)reply.getOutput().get(0)).booleanValue();
		}
		else{
			throw new Exception("Request failed, Status: " + reply.getStatus());
		}
	}

	@Override
	public boolean reserveRoom(int id, int customer, String location) throws Exception {
		LinkedList<Object> args = new LinkedList<Object>();
		args.add(id);
		args.add(customer);
		args.add(location);
		
		Request request = new Request("reserveRoom",args);
		Reply reply = sendRequest(request);
		if(reply.getStatus()==Reply.STATUS_SUCCESS){
			return ((Boolean)reply.getOutput().get(0)).booleanValue();
		}
		else{
			throw new Exception("Request failed, Status: " + reply.getStatus());
		}
	}

	@SuppressWarnings("rawtypes")
	@Override
	public boolean itinerary(int id, int customer, Vector flightNumbers,String location, boolean Car, boolean Room) throws Exception {
		LinkedList<Object> args = new LinkedList<Object>();
		args.add(id);
		args.add(customer);
		args.add(flightNumbers);
		args.add(location);
		args.add(Car);
		args.add(Room);
		
		Request request = new Request("itinerary",args);
		Reply reply = sendRequest(request);
		if(reply.getStatus()==Reply.STATUS_SUCCESS){
			return ((Boolean)reply.getOutput().get(0)).booleanValue();
		}
		else{
			throw new Exception("Request failed, Status: " + reply.getStatus());
		}
	}
	
	
}
